package io;

import graphics.datastructures.Model;
import graphics.datastructures.SimpleMesh;
import graphics.datastructures.Triangle;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

/**
 * 
 */
public class SimpleReader implements ModelReader {
	private Scanner _scanner;
	private Map<Integer, float[]> _vertices = new HashMap<Integer, float[]>();
	private Collection<int[][]> faces = new ArrayList<int[][]>();
	private Map<Integer, float[]> _normals = new HashMap<Integer, float[]>();
	private int _vertexCount;
	private int _normalCount;
	private File _file;
	private SimpleMesh model;

	public SimpleReader(String file) throws FileNotFoundException {
		_file = new File(file);
		_scanner = new Scanner(_file);
		_vertexCount = 1;
		_normalCount = 1;
	}

	@Override
	public Model read() {
		model = new SimpleMesh();
		while (_scanner.hasNext()) {
			handleString(_scanner.nextLine());
		}

		Model m2 = model;
		model = new SimpleMesh();
		return m2;
	}

	private Triangle genTriangle(int[][] face) {
		float[][] value = new float[3][];
		for (int i = 0; i < value.length; i++)
			value[i] = _vertices.get(face[0][i]);
		float[][] normals = new float[3][];
		for (int i = 0; i < normals.length; i++)
			normals[i] = _normals.get(face[1][i]);
		float[][] color = new float[3][];
		float[] randomc = randomcolor();
		for (int i = 0; i < color.length; i++) {
			randomc = new float[3];
			for (int j = 0; j < randomc.length; j++)
				randomc[j] = 1;

			color[i] = randomc;
		}
		return new Triangle(value, normals);
	}

	private float[] randomcolor() {

		return null;
	}

	private void handleString(String line) {
		if (line.matches("v .*"))
			handleVertex(line);
		if (line.matches("vn .*"))
			handleVertexNormal(line);
		if (line.matches("f [0-9]*/[0-9]*/[0-9]* [0-9]*/[0-9]*/[0-9]* [0-9]*/[0-9]*/[0-9]*")) // simple
			handleFace(line); // face
		if (line.matches("f [0-9]*//[0-9]* [0-9]*//[0-9]* [0-9]*//[0-9]*"))
			handleFace2(line);
		if (line.matches("vt "))
			;//

		// donothing
	}

	private void handleFace2(String line) {
		Scanner s = new Scanner(line);
		int[] face = new int[3];
		int[] normal = new int[3];
		s.skip("f ");
		for (int i = 0; i < 3; i++) {
			String t = s.next("([0-9])*(//)([0-9])*");
			String[] ar = t.split("(//)");
			face[i] = new Integer(ar[0]);
			normal[i] = new Integer(ar[1]);
		}
		int[][] rv = new int[2][];
		rv[0] = face;
		rv[1] = normal;
		model.add(genTriangle(rv));

	}

	private void handleVertexNormal(String line) {
		Scanner s = new Scanner(line);
		s.skip("vn ");
		float x = new Float(s.next("([0-9]|.|-)*"));
		float y = new Float(s.next("([0-9]|.|-)*"));
		float z = new Float(s.next("([0-9]|.|-)*"));
		_normals.put(_normalCount++, ar(x, y, z));
	}

	private void handleVertex(String line) {
		Scanner s = new Scanner(line);
		s.skip("v ");
		float x = new Float(s.next("([0-9]|.|-)*"));
		float y = new Float(s.next("([0-9]|.|-)*"));
		float z = new Float(s.next("([0-9]|.|-)*"));
		_vertices.put(_vertexCount++, ar(x, y, z));
	}

	private void handleFace(String line) {
		Scanner s = new Scanner(line);

		int[] face = new int[3];
		int[] normal = new int[3];
		s.skip("f ");
		for (int i = 0; i < 3; i++) {
			String t = s.next("([0-9])*(/)([0-9])*(/)([0-9])*");
			String[] ar = t.split("(/)");
			face[i] = new Integer(ar[0]);
			normal[i] = new Integer(ar[2]);
		}
		int[][] rv = new int[2][];
		rv[0] = face;
		rv[1] = normal;

		model.add(genTriangle(rv));
	}

	public static float[] ar(float... floats) {
		return floats;
	}
}
